package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-11-16
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/eduservice/edu-teacher")
@CrossOrigin //解决跨域问题
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * 查询所有讲师
     * @return
     */
    //rest风格
    @ApiOperation(value="所有讲师")
    @GetMapping("/findAll")
    public R findAllTeacher()
    {
//        try{
//            int i=10/0; //模拟异常，进入全局异常处理器
//        }catch(Exception e)
//        {
//            throw new SelfExceptionHandler("自定义异常处理器",203);
//        }

        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }

    /**
     * 删除指定id的教师，逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public R removeTeacher(@PathVariable String id)
    {
        boolean b = eduTeacherService.removeById(id);
        if(b==true)
        return R.ok();
        else
            return R.error();

    }

    /**
     * 分页查询
     * @param current
     * @param limit
     * @return
     */
    @GetMapping("/pageList/{current}/{limit}")
    public R pageList(@PathVariable long current,@PathVariable long limit)
    {
        Page<EduTeacher> pageInfo=new Page<>(current,limit);

        pageInfo =( Page<EduTeacher>) eduTeacherService.page(pageInfo, null);
        long total = pageInfo.getTotal();
        Map<String,Object> map=new HashMap<>();
        map.put("total",total);
        map.put("items",pageInfo);

            return R.ok().data(map);
    }

    /**
     * 根据条件查询教师
     * @param current
     * @param limit
     * @param teacherQuery  可以为null
     * @return
     */
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,@PathVariable long limit,@RequestBody(required = false)
            TeacherQuery teacherQuery)
    {

        Page<EduTeacher> pageInfo = new Page<>(current, limit);
        pageInfo=eduTeacherService.pageTeacher(pageInfo,teacherQuery);
        long total = pageInfo.getTotal();//总记录数
        List<EduTeacher> records=pageInfo.getRecords();//数据list集合
        return R.ok().data("total",total).data("rows",records);

    }

    /**
     * 添加讲师
     * 路径前面没有那个/也可以
     * @param eduTeacher
     * @return
     */
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher)
    {

        boolean save = eduTeacherService.save(eduTeacher);
        if(save==true)
        {
            return R.ok();
        }
        return R.error();

    }

    @GetMapping("/getTeacher/{id}")
    public R getTeacher(@PathVariable String id)
    {
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",teacher);


    }


    /**
     * 讲师修改功能
     * @param eduTeacher
     * @return
     */
 @PutMapping("/modifyTeacher")
public R modifyTeacher(@RequestBody EduTeacher eduTeacher)
 {
     boolean b = eduTeacherService.updateById(eduTeacher);
     if(b==true)
     {
         return R.ok();
     }
     return R.error();

 }





}

